package zack.project.types.design.framework.tree;


import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

public  abstract class AbstractMutliThreadStrategyRouter<T,D,R> implements StrategyHandler <T,D,R> ,StrategyMapper<T,D,R>{

    @Getter
    @Setter
    protected StrategyHandler<T,D,R>  DefaultStrategyHandler= StrategyHandler.DEFAULT;

    public R router(T requestParameter,D dynamicContext) throws Exception {
        StrategyHandler<T, D, R> StrategyHandler = get(requestParameter, dynamicContext);
        if( null  != StrategyHandler )
            return StrategyHandler.apply(requestParameter,dynamicContext);
        return (R) DefaultStrategyHandler.apply(requestParameter,dynamicContext);
    }

    @Override
    public R apply(T requestParameter, D dynamicContext) throws Exception {

      mutliThread(requestParameter,dynamicContext);

      return doApply(requestParameter,dynamicContext);
    }
    protected abstract void mutliThread(T requestParameter,D dynamicContext)throws ExecutionException, InterruptedException, TimeoutException;
    protected abstract R doApply(T requestParameter, D dynamicContext) throws Exception;
}
